package com.apes.scm.account.account.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.scm.account.account.model
 * @ClassName: BatchOfFrozen
 * @Author: pyb
 * @Description: 批次冻结数明细账
 * @CreateTime: 2020-12-01 15:46
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = BatchOfFrozen.CONST_TABLE_NAME)
public class BatchOfFrozen extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_batch_of_frozen";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "冻结数明细账")
    @Comment("内码")
    private Long id;

    @Column(name = "batch_id", length = 32, nullable = false)
    @Comment("批次号")
    private Long batchId;

    @Column(name = "batch_number", length = 32, nullable = false)
    @Comment("批次内码")
    private Long batchNumber;

    @Column(name = "source_business_type", length = 32)
    @Comment("来源单据类型")
    private String sourceBusinessType;

    @Column(name = "ORDER_NUMBER",length = 32)
    @Comment("单据号码")
    private String orderNumber;

    @Column(name = "ORDER_SERIAL_NUMBER", length = 30)
    @Comment("单据序号")
    private String orderSerialNumber;

    @Column(name = "SOURCE_TYPE", length = 32)
    @Comment("源单据类型")
    private String sourceType;

    @Column(name = "SOURCE", length = 32)
    @Comment("源单据号码")
    private String source;

    @Column(name = "SOURCE_NUMBER", length = 30)
    @Comment("源单据序号")
    private String sourceNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "id", nullable = false)
    @Comment("商品编码")
    private Product product;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    @Comment("计量单位")
    private ProductUom uom;

    @DateTime
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CHANGE_TIME")
    @Comment("变动时间")
    private java.util.Date changeTime;

    @Column(name = "CHANGE_QTY", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("变动数量")
    private double changeQty = 0.0;

    @Column(name = "FROZEN_QTY", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("冻结数量")
    private double frozenQty = 0.0;

    @Column(name = "THAWING_QTY", nullable = false, columnDefinition = "NUMBER(16,4) DEFAULT 0")
    @Comment("解冻数量")
    private double thawingQty = 0.0;

    @Column(name = "NOTE", nullable = false, length = 256)
    @Comment("备注")
    private String remarks;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "COMPANY_ID", referencedColumnName = "id", nullable = false)
    @Comment("公司编码")
    private PartyRole company;

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;  //JPA乐观锁

    public void addThawingQty(double qty) {
        this.thawingQty += qty;
//        if (thawingQty < 0) throw new RuntimeException("批次解冻数不能小于0，批次号："+batchId+"商品编码："+product.getId());
//        if (thawingQty > changeQty) throw new RuntimeException("批次解冻数不能大于冻结数，批次号："+batchId+"商品编码："+product.getId());
    }
}
